// pages/opt_user_admin/opt_page/hospital_base_info/base_info/base_info.js
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../common/constant_data.js')
var uitl = require('../../../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    windowHeight:0,
    ownBack:true,
    otherBack:true,
    overDays:0,
    ownNoBack:true,
    otherNoBack:true,
    overNoBackDays:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          windowHeight: res.windowHeight,
        });
      }

    });

    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var typeId = options.typeId;
    var hospitalName = options.hospitalName;
    that.setData({
      hospitalName: hospitalName,      
      typeId: typeId
    })

    that.loadData(); 


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     this.setData({
       winHeight: this.data.winHeight + this.data.windowHeight
     })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loadData:function() {
    
    var that = this;
    var hospitalName = that.data.hospitalName;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/hospital_info/base_info',
        data: {
          tid: that.data.typeId
        },
        header: header,
        success: function (res) {
          getApp().hideLoading();
          console.log("baseinfo---->", res);
          var homingInfoList = res.data.homingInfoList;
          var chairInfoList = res.data.chairInfoList;
          var homingLastBackList = res.data.homingLastBackList;
          var noBackChairList = res.data.noBackChairList;
          var currentOnPipleChairList = res.data.currentOnPipleChairList;

          var len = homingInfoList.length;
          if (len > 4) {
            var height = that.data.winHeight - 31;
            var addHeight = (height / 4) * (len - 3);
            that.setData({
              winHeight: height + addHeight
            })
          }

          var pipleCount = 0;
          for (var i = 0; i < homingInfoList.length; i++) {
            pipleCount += homingInfoList[i].counts;
          }
          var chairCount = 0;
          var chairList = new Array();
          for (var i = 0; i < chairInfoList.length; i++) {
            chairCount += chairInfoList[i].counts;
            if(!chairInfoList[i].ids) {
               continue;
            }
            var arr = chairInfoList[i].ids.split(",");
            chairList.push.apply(chairList, arr);
          }

          var noBackCount = noBackChairList.length;
          var backChairCount = currentOnPipleChairList.length;

          //统计到的轮椅 
          var recordChairList = new Array();

          for (var i = 0; i < homingLastBackList.length; i++) {
            homingLastBackList[i].last_back_time = uitl.timestampToTime(homingLastBackList[i].last_back_time);
          }


          var ownNoBackChairCount = 0;
          var otherNoBackChairCount = 0;
          var ownNoBackChairList = new Array();
          var otherNoBackChairList = new Array();
          for (var i = 0; i < noBackChairList.length; i++) {
            noBackChairList[i].uur_open_lock_time = uitl.timestampToTime(noBackChairList[i].uur_open_lock_time);

            noBackChairList[i].last_update = uitl.timestampToTime(noBackChairList[i].last_update);

            recordChairList.push(noBackChairList[i].uur_equipmnet_id);

            if (noBackChairList[i].hp_hpspital == hospitalName) {
              ownNoBackChairCount++;
              ownNoBackChairList.push(noBackChairList[i]);
            } else {
              otherNoBackChairCount++;
              otherNoBackChairList.push(noBackChairList[i]);
            }

          }

          var ownChairCount = 0;
          var otherChairCount = 0;
          var ownBackChairList = new Array();
          var otherBackChairList = new Array();
          for (var i = 0; i < currentOnPipleChairList.length; i++) {
            currentOnPipleChairList[i].back_timestamp = currentOnPipleChairList[i].uur_receive_return_time;
            currentOnPipleChairList[i].uur_receive_return_time = uitl.timestampToTime(currentOnPipleChairList[i].uur_receive_return_time);

            recordChairList.push(currentOnPipleChairList[i].uur_equipmnet_id);

            if (currentOnPipleChairList[i].ti_name == hospitalName) {
              ownChairCount++;
              ownBackChairList.push(currentOnPipleChairList[i]);
            } else {
              otherChairCount++;
              otherBackChairList.push(currentOnPipleChairList[i]);
            }
          }
          console.log("baseinfo---->6");
          var commonAarr = new Array();
          for (var i = 0; i < chairList.length; i++) {
            var flg = false;
            for (var j = 0; j < recordChairList.length; j++) {
              if (recordChairList[j].toString() == chairList[i].toString()) {
                flg = true;
                break;
              }
            }
            if (flg) {
              commonAarr.push(chairList[i]);
            }
          }
          var diffArr = new Array();
          for (var i = 0; i < chairList.length; i++) {
            var flg = false;
            for (var j = 0; j < commonAarr.length; j++) {
              if (chairList[i].toString() == commonAarr[j].toString()) {
                flg = true;
                break;
              }
            }
            if (!flg) {
              diffArr.push(chairList[i]);
            }
          }


          for (var i = 0; i < recordChairList.length; i++) {
            var flg = false;
            for (var j = 0; j < commonAarr.length; j++) {
              if (recordChairList[i].toString() == commonAarr[j].toString()) {
                flg = true;
                break;
              }
            }
            if (!flg) {
              diffArr.push(recordChairList[i]);
            }
          }

          console.log("--------diffArr>", diffArr);


          that.setData({
            homingInfoList: homingInfoList,
            chairInfoList: chairInfoList,
            homingLastBackList: homingLastBackList,
            noBackChairList: noBackChairList,
            noBackChairOriginList: noBackChairList,
            currentOnPipleChairList: currentOnPipleChairList,
            currentOnPipleChairOriginList: currentOnPipleChairList,
            ownBackChairList: ownBackChairList,
            otherBackChairList: otherBackChairList,
            ownNoBackChairList: ownNoBackChairList,
            otherNoBackChairList: otherNoBackChairList,
            diffChairList: diffArr.join(","),
            diffChairCount:diffArr.length,
            pipleCount: pipleCount,
            chairCount: chairCount,
            noBackCount: noBackCount,
            backChairCount: backChairCount,
            ownChairCount: ownChairCount,
            otherChairCount: otherChairCount,
            ownNoBackChairCount: ownNoBackChairCount,
            otherNoBackChairCount: otherNoBackChairCount,
            ownBack: true,
            otherBack: true,
            ownNoBack: true,
            otherNoBack: true,
          })
        }
      })
    });  

  },

  /**
     * 滑动切换tab
     */
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },


  /**
   * 未归还的数据
   */
  noBackSearch:function(e) {
    var vals = e.detail.value;
    var v = 0;
    for (var i = 0; i < vals.length; i++) {
      v += parseInt(vals[i]);
    }    

    this.getNoBackList(v);

  },

  /**
   * 归还tab过滤数据
   */
  backSearch:function(e) {
    var vals = e.detail.value;
    var v = 0;
    for(var i=0;i<vals.length;i++){
      v += parseInt(vals[i]);
    }    

    this.getBackList(v);

    if (this.data.overDays > 0) {
      this.getOverDaysNoUseList();
    }
    
  },

  getBackList:function(v) {

    switch (v) {
      case 0:
        this.setData({
          currentOnPipleChairList: [],
          ownBack: false,
          otherBack: false
        })
        break;
      case 1:
        this.setData({
          currentOnPipleChairList: this.data.ownBackChairList,
          ownBack: true,
          otherBack: false
        })
        break;
      case 2:
        this.setData({
          currentOnPipleChairList: this.data.otherBackChairList,
          ownBack: false,
          otherBack: true
        })
        break;
      case 3:
        this.setData({
          currentOnPipleChairList: this.data.currentOnPipleChairOriginList,
          ownBack: true,
          otherBack: true
        });
        break;
    }

  },


  getNoBackList: function (v) {

    switch (v) {
      case 0:
        this.setData({
          noBackChairList: [],
          ownNoBack: false,
          otherNoBack: false
        })
        break;
      case 1:
        this.setData({
          noBackChairList: this.data.ownNoBackChairList,
          ownNoBack: true,
          otherNoBack: false
        })
        break;
      case 2:
        this.setData({
          noBackChairList: this.data.otherNoBackChairList,
          ownNoBack: false,
          otherNoBack: true
        })
        break;
      case 3:
        this.setData({
          noBackChairList: this.data.noBackChairOriginList,
          ownNoBack: true,
          otherNoBack: true
        });
        break;
    }

  },



  /**
   * 超出多少天后未被租借
   */
  overDaysNoUse:function(e) {
    this.setData({
      overDays: e.detail.value
    });
    var v = 0;
    if (this.data.ownBack && this.data.otherBack) {
      v = 3;
    } else if (this.data.ownBack) {
      v = 1;
    }else {
      v = 2;
    }
    this.getBackList(v);
    console.log("---overDays",this.data.overDays);
    this.getOverDaysNoUseList();
  },

  /**
   * 获取超过一定天数未使用记录
   */
  getOverDaysNoUseList:function() {
    if (!this.data.overDays) {
         return;
    }
    var overTimeLen = this.data.overDays * 24 * 60 * 60 * 1000;
    var currentTime = new Date().getTime();
    var list = this.data.currentOnPipleChairList;
    var overList = new Array();
    for (var i=0;i<list.length;i++) {
      var diff = currentTime - list[i].back_timestamp;
      console.log("-----timeLen", diff, currentTime, list[i].back_timestamp);
      if(diff >= overTimeLen) {
          overList.push(list[i]);
      }  
    }

    this.setData({
      currentOnPipleChairList: overList
    })

  }




})